﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace DelicateDict
{
    public partial class MyStudy : Form
    {
        DataTable dt;
        public MyStudy()
        {
            InitializeComponent();
        }

        private void MyStudy_Load(object sender, EventArgs e)
        {
            Load_Treeview();          
        }
        private void Load_Treeview()
        {
            DataTable dt = mystudy.SelectSum();
            foreach (DataRow row in dt.Rows)
            {
                List<string> l = new List<string>();
                foreach (DataColumn col in dt.Columns)
                {
                    l.Add(row[col].ToString());
                }
                TreeNode node = new TreeNode(l[0] + " (" + l[1] + " words ) ");
                treeView1.Nodes.Add(node);
            }
        }
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
           
            string dateTime = e.Node.Text.Substring(0, 7);
            //MessageBox.Show(dateTime);
            dt = mystudy.SelectWords(dateTime);
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // load mean
            string mean = mystudy.SelecMean(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
            //MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
            // load decorate
            Classes.DecorateMean.SetColor(ref mean);

            //get content html
            string ImagePath = Environment.CurrentDirectory + @"\WebBrowserContent";
            string HtmlDocument = File.ReadAllText(Environment.CurrentDirectory + @"\WebBrowserContent\mystudy.html");
            ImagePath = ImagePath.Replace('\\', '/');
            HtmlDocument = HtmlDocument.Replace("path", ImagePath);

            // insert content into html
            HtmlDocument = HtmlDocument.Replace("MeanSettingColor", mean);
            //
            //File.WriteAllText(Environment.CurrentDirectory + @"\WebBrowserContent\browserContent.html", HtmlDocument);
            //webBrowserContent.Url = new System.Uri(Environment.CurrentDirectory + @"\WebBrowserContent\browserContent.html");     
            webBrowser1.DocumentText = HtmlDocument;
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // check row
            if (dataGridView1.RowCount == 0) return;

            mystudy.Delete(dataGridView1.CurrentCell.Value.ToString());
           // dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
            dt.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
            //dt.Clear();
            dataGridView1.Refresh();
        }

        private void deleteThisToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount == 0) return;
            mystudy.DeleteMonth(dataGridView1.CurrentCell.Value.ToString());
            // dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
            dt.Rows.Clear();
            //dt.Clear();
            dataGridView1.Refresh();
        }

        private void resetAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount == 0) return;
            mystudy.DeleteAll();
            // dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
            dt.Clear();
            //dt.Clear();
            dataGridView1.Refresh();
        }
    }
}
